const crypto = require('crypto')
const jwtweb = require('jsonwebtoken')
const {PRIVATE_KEY} = require('./constant')

function md5(s) {
    //注意参数需要为String类型，否则会出错
    return crypto.createHash('md5')
        .update(String(s)).digest('hex')
}

function decoded(req) {
    const authorization = req.get('Authorization')
    let token = ''
    if (authorization.indexOf('Bearer', '') === 0) {
        token = authorization.replace('Bearer ', '')
    } else {
        token = authorization
    }
    return jwtweb.verify(token, PRIVATE_KEY)
}

module.exports = {
    md5,
    decoded,
}
